<?php
	class Basic {
		public static function pr($arr=array()){
			print_r($arr);
		}
		public static function eco($str=""){
			echo $str;
		}
		public static function need($file){
			Basic::fileExist($file)?require_once($file):die("Error: Import file error.");
		}
		public static function keyExist($key="",$array=array()){
			return array_key_exists($key,$array);
		}
		public static function createFile($fil="no_name"){
			return fopen($fil, "a");
		}
		public static function pathInfo($path){
			return pathinfo($path);
		}
		public static function isContainOperator($string){
			$operators = array("+","-","*","/","%","=","<",">","<=",">=","!=");
			return Basic::isContain($string, $operators);
		}
		public static function arrayValueExist($value,$array){
			foreach($array as $key => $val ){
				if(Basic::isEqual($value,$val))return true;
			}
			return false;
		}
		public static function isEqual($str1,$str2){
			$help=false;
			if(strlen($str1)==strlen($str2)){
				//for($i=0;$i<strlen($str1);$i++)if(strcmp(substr($str1,$i,1),substr($str2,$i,1))===0)
				for($i=0;$i<strlen($str1);$i++){
					if(strcmp($str1[$i],$str2[$i])!==0){$help=false;break;}
					else $help=true;
				}
			}
			return $help;
		}
		public static function isContain($string,$chars){
			if(is_array($chars)){
				foreach($chars AS $char ) if(strpos($string,$char)!==false)return true;
			}else if(is_string($chars)){
				return strpos($string,$chars)!==false?true:false;
			}
			return false;
		}
		public static function deleteFile($file){
			return unlink($file);
		}
		public function getPath($path=array("page"=>"index"),$vars=array()){
			if(!empty($path)){
				if(isset($_SERVER["QUERY_STRING"])&&$_SERVER["QUERY_STRING"]!==""){
					$link = array();
					foreach($path as $controller =>$action ){
						$link[]="com=".$controller."&action=".$action;
						foreach($vars as $key =>$value )$link[]=$key."=".$value;
						$link = "index.php?".implode("&",$link) ;
						break;
					}
				}
				else{
					foreach($path as $controller =>$action ){
						$link = "index.php/".$controller."/".$action;
						foreach($vars as $key=>$val)$link.="/".$key.":".$val;
						break;
					}
				}
				return ROOT . $link;
			}
		}
		public function redirect($path=array("page"=>"index"),$vars=array()){
			header("location:".Basic::getPath($path,$vars));
		}
		public function refresh($path=array("page"=>"index"),$vars=array(),$second=3){
			header("refresh:".$second.",url=".Basic::getPath($path,$vars));
		}
		public static function fileInfo($file){
			return array( 
				"size"=>filesize($file),
				"owner"=>fileowner($file),
				"group"=>filegroup($file),
				"create"=>filectime($file),
				"modifie"=>filemtime($file),
				"access"=>fileatime($file),
				"perms"=>fileperms($file),
				"type"=>filetype($file),
				"status"=>stat($file),
				"basename"=>basename($file),
				"dirname"=>dirname($file),
			);
		}
		public static function mimeContentType($file){
			return pathinfo($file, PATHINFO_EXTENSION);
		}
		public static function fileSize($file){
			return filesize($file);
		}
		public static function openFolder($folder="."){
			return opendir($folder);
		}
		public static function readFolder($handle=false){
			return readdir($handle);
		}
		public static function closeFolder($handle=false){
			closedir($handle);
		}
		public static function createFolder($folder=""){
			return mkdir($folder);
		}
		public static function getFolders($folder="."){
			$handle=Basic::openFolder($folder);
			$files = array();
			if($handle!==false){
				while(($file=Basic::readFolder($handle))!==false){
					if(is_dir($file))$files[]=$file;
				}
			}
			return $files;
		}
		public static function getFiles($folder="."){
			$handle=Basic::openFolder($folder);
			$files = array();
			if($handle!==false){
				while(($file=Basic::readFolder($handle))!==false){
					if(is_file($folder.$file))$files[]=$file;
				}
			}
			return $files;
		}
		public static function fileExist($fil){
			return file_exists($fil);
		}
		public static function fileGetContent($file){
			return file_get_contents($file);
		}
		public static function fileCopy($src="",$des=""){
			return copy($src,$des);
		}
		public static function sizeFormat($size){
			$temp=$size/1024;
			if ($temp<1024){
				return (int)$temp . "." . ($size%1024) . " KB" ;
			}else{
				$kb = $size/1024;
				$size= $kb/1024;
				return (int)$size.".".($size%1024). " MB";
			}
		}
		public static function getPlural($str=""){
			$plural="";
			$case = substr($str,strlen($str)-1);
			switch($case){
				case "y": 
					$plural = substr($str,0,strlen($str)-1)."ies";
				break;
				case "s":
					$plural = substr($str,0,strlen($str)-1)."es";
				break;
				default:
					$plural = $str."s";
					break;
			}
			return $plural;
		}
		public static function dateCompare($date1,$date2){
			return strtotime($date1)-strtotime($date2);
		}
		public static function leapsDate($start_date, $end_date){
			$days=intval(strtotime($end_date)-strtotime($start_date))/86400;
			$hours=intval((strtotime($end_date)-strtotime($start_date))%86400/3600);
			return array("days"=>$days,"hours"=>$hours);
		}
	}
